﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Brain.IO;
using System.IO;

namespace Brain.Editor
{
    public partial class MakeWorldObjectForm : Form
    {
        private Random random;

        private DefaultContentManager content;
        public MakeWorldObjectForm()
        {
            random = new Random();
            InitializeComponent();

            this.modelControl1.OnDraw += new SetVoid(terrainControl1_OnDraw);

            content = new DefaultContentManager();
        }

        bool optionChanged = false;
        SceneContentFile selectedFile;
        void terrainControl1_OnDraw()
        {
            if (radio_Model.Checked)
            {
                if (optionChanged && selectedFile != null)
                {
                    if (this.modelControl1.WorldObject == null)
                    {
                        this.modelControl1.WorldObject = new WorldObject();
                    }
                    if (this.check_Animated.Checked)
                    {
                        this.modelControl1.WorldObject.ModelPlace = "skin:confile:" + selectedFile.Name;
                    }
                    else
                    {
                        this.modelControl1.WorldObject.ModelPlace = "confile:" + selectedFile.Name;
                    }

                    optionChanged = false;
                }
            }
            else if (radio_BrainModel.Checked)
            {
                if (optionChanged && selectedFile != null)
                {
                    if (this.modelControl1.WorldObject == null)
                    {
                        this.modelControl1.WorldObject = new WorldObject();
                    }

                    optionChanged = false;
                }
            }
        }

        

        private void radio_Model_CheckedChanged(object sender, EventArgs e)
        {
            optionChanged = true;
        }

        private void button_ToAsset_Click(object sender, EventArgs e)
        {
            WorldObject wo = this.modelControl1.WorldObject;
            if (wo != null)
            {
                GameScreen gs = Engine.Instance.ScreenManager.GetScreen(0);
                gs.AddComponent(wo);
                wo.Rendering = WorldObjectRendering.Rendered;

                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void button_AssetBrowser_Click(object sender, EventArgs e)
        {
            ContentManagerForm manager = new ContentManagerForm(true);
            if (manager.ShowDialog() == DialogResult.OK)
            {
                SceneContentFile file = manager.SelectedFile;
                this.textBox1.Text = file.Name;
                this.optionChanged = true;

                this.selectedFile = file;
            }
        }
    }
}
